//
//  ViewController.m
//  VLCDEMO
//
//  Created by tans on 2017/6/5.
//  Copyright © 2017年 cn.jsfund. All rights reserved.
//

#import "ViewController.h"
#import <MobileVLCKit/MobileVLCKit.h>

@interface ViewController ()<VLCMediaThumbnailerDelegate>
@property (weak, nonatomic) IBOutlet UIView *playView;
@property (weak, nonatomic) IBOutlet UIImageView *bgIV;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UISlider *durationSlider;

@end

@implementation ViewController
{
    VLCMediaPlayer *player;
    VLCMediaThumbnailer *thumnailer;
}
- (IBAction)playClicked:(UIButton *)sender {
    if ([[sender titleForState:UIControlStateNormal] isEqualToString:@"Play"]) {
        [player play];
        [sender setTitle:@"Pause" forState:UIControlStateNormal];
        
//        thumnailer.snapshotPosition ++;
        [thumnailer fetchThumbnail];
    } else {
        [player pause];
        [sender setTitle:@"Play" forState:UIControlStateNormal];
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    player = [[VLCMediaPlayer alloc] initWithOptions:nil];
    player.drawable = _playView;
    //http://183.60.197.34/12/d/v/z/g/dvzgayotjmpakufiqctmqgosxchtvi/he.yinyuetai.com/6F77014B7C496F44BFDABCD2F2331F1D.flv
    //rtsp://218.204.223.237:554/live/1/66251FC11353191F/e7ooqwcfbqjoo80j.sdp
    //rtmp://live.hkstv.hk.lxdns.com/live/hks
    player.media = [VLCMedia mediaWithURL:[NSURL URLWithString:@"rtmp://live.hkstv.hk.lxdns.com/live/hks"]];
    player.time = [VLCTime timeWithInt:30];
    
    thumnailer = [VLCMediaThumbnailer thumbnailerWithMedia:player.media andDelegate:self];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(VLCMediaPlayerTimeChanged:) name:VLCMediaPlayerTimeChanged object:nil];
    
}

- (void)VLCMediaPlayerTimeChanged:(NSNotification *)notif
{
    _timeLabel.text = player.time.stringValue;
    _durationSlider.value = player.position;
}

- (void)mediaThumbnailerDidTimeOut:(VLCMediaThumbnailer *)mediaThumbnailer
{
    NSLog(@"thumbnail time out");
}

- (void)mediaThumbnailer:(VLCMediaThumbnailer *)mediaThumbnailer didFinishThumbnail:(CGImageRef)thumbnail
{
    _bgIV.image = [UIImage imageWithCGImage:thumbnail];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
